//准备函数
jQuery(document).ready(function() {
    alert("111")
    //姓名数据
    var xmJh;
    //明细表请假类型数据
    var qjlxJh;
    //明细表时长数据
    var scJh;
    //明细表开始日期数据
    var ksrqJh;

    //获取主表请假类型的field
    var cglxFieldId = WfForm.convertFieldNameToId("qjlx");
    //获取主表累计已休产检假统计的field
    var ljtjjFieldId = WfForm.convertFieldNameToId("ljyxcjjtj");
    //默认 字段赋值为隐藏
    WfForm.changeFieldAttr(ljtjjFieldId, 4);

    //请假类型发生改变   触发函数
    WfForm.bindFieldChangeEvent(cglxFieldId,
    function() {
        //获取主表请假类型的field的值
        var mainCglx = WfForm.getFieldValue(cglxFieldId);
        //判断请假类型是否为产检假（14）
        if (mainCglx == 14) {
            //给字段赋值为只读
            WfForm.changeFieldAttr(ljtjjFieldId, 1);
        } else {
            //给字段赋值为隐藏
            WfForm.changeFieldAttr(ljtjjFieldId, 4);
        }
    });

    checkCustomize = function() {
        alert("请假类型111")
        //获取主表请假类型的field的值
        var mainCglx = WfForm.getFieldValue(cglxFieldId);
        alert("请假类型:" + mainCglx)
        if (mainCglx == 14) {
            fO();
        }
        alert("请假类型:222" + mainCglx)
        if (mainCglx == 7) {
            fT();
        }

        var qjts;
        var flag = "";
        var sub = jQuery("#submitdtlid0").val();
        if (sub != null) {
            var arr = sub.split(",");
            for (var a = 0; a < arr.length; a++) {
                qjts = ((jQuery("#field10430_" + arr[a])).val()) * 1;
                if (qjts > 5) {
                    flag = "true";
                    break;
                }
            }
        }

        if (flag == "true") {
            jQuery("#field27325").val("0");
        } else {
            jQuery("#field27325").val("1");
        }
        //控制哺乳假不允许提交
        var qjlx = jQuery("#field17940").val();
        if (qjlx == 12) {
            alert("哺乳假禁止手动提交!");
            return false;
        }


        alert("请假类型:333" + mainCglx)
        //测试
        return false;
    }
    function fO() {
        //校验函数
        //获取明细表1的fieldid
        var sub = jQuery("#submitdtlid0").val();
        //获取主表请假类型的field的值
        var mainCglx = WfForm.getFieldValue(cglxFieldId);

        //判断请假类型是否为产检假（14）
        if (mainCglx == 14) {
            //判断明细表是否为null
            if (sub != null) {
                //split 分组
                var arr = sub.split(",");
                //循环明细表获取数据
                for (var i = 0; i < arr.length; i++) {
                    //获取明细表姓名数据
                    var xm = ((jQuery("#field10423_" + arr[i])).val());
                    xmJh = xmJh + "," + xm;
                    //获取明细表请假类型
                    var qjlx = ((jQuery("#field10425_" + arr[i])).val());
                    qjlxJh = qjlxJh + "," + qjlx;
                    //获取明细表时长数据
                    var sc = ((jQuery("#field10430_" + arr[i])).val());
                    if (sc > 0.5) {
                        alert("产检假每次请假不能超过0.5天"); return false;
                    }
                    scJh = scJh + "," + sc;
                    //获取明细表开始日期数据
                    var ksrq = ((jQuery("#field10426_" + arr[i])).val());
                    ksrqJh = ksrqJh + "," + ksrq;
                }

                var flag = true;
                //ajax传值并根据返回值给表单字段赋值
                jQuery.ajax({
                    url: "/api/weavernorth/action/sick/Prenatal/getData",
                    data: {
                        "qjlx": qjlxJh,
                        "xm": xmJh,
                        "sc": scJh,
                        "ksrq": ksrqJh
                    },
                    dataType: 'text',
                    async: false,
                    success: function(data) {
                        var lj = data.split(",");
                        //赋值
                        WfForm.changeFieldValue(ljtjjFieldId, {
                            value: lj[1]
                        });
                        //提示
                        if (lj[0] == "true") {

} else {
                            alert("每天只能请一次产检假");

                            flag = false;
                        }

                    },
                    error: function(data) { //请求失败时被调用的函数
                        alert("加载失败，请联系管理员！")
                    }
                })

            }
        }

        return flag;

    }

    function fT() {
        //校验函数
        //姓名数据
        var xmJh;
        //明细表请假类型数据
        var qjlxJh;
        //明细表时长数据
        var scJh;
        //明细表开始日期数据
        var ksrqJh;
        //获取主表请假类型
        var mainLx = jQuery("#field17940").val();
        //获取明细表1的fieldid
        var sub = jQuery("#submitdtlid0").val();
        //判断请假类型是否为病假（7）
        if (mainLx == 7) {
            //判断明细表是否为null
            if (sub != null) {
                debugger;
                //split 分组
                var arr = sub.split(",");
                //循环明细表获取数据
                for (var i = 0; i < arr.length; i++) {
                    //获取明细表姓名数据
                    var xm = ((jQuery("#field10423_" + arr[i])).val());
                    xmJh = xmJh + "," + xm;
                    //获取明细表请假类型
                    var qjlx = ((jQuery("#field10425_" + arr[i])).val());
                    qjlxJh = qjlxJh + "," + qjlx;
                    //获取明细表时长数据
                    var sc = ((jQuery("#field10430_" + arr[i])).val());
                    scJh = scJh + "," + sc;
                    //获取明细表开始日期数据
                    var ksrq = ((jQuery("#field10426_" + arr[i])).val());
                    ksrqJh = ksrqJh + "," + ksrq;
                }

                //ajax传值并根据返回值给表单字段赋值
                jQuery.ajax({
                    url: "/api/weavernorth/action/sick/Sick/getData",
                    data: {
                        "qjlx": qjlxJh,
                        "xm": xmJh,
                        "sc": scJh,
                        "ksrq": ksrqJh
                    },
                    dataType: 'text',
                    async: false,
                    success: function(data) {
                        alert(data);
                        alert(data == 'true');

                        //对应表单字段赋值
                        if (data == "true") {
                            //给字段赋值为必填且阻止流程提交
                            WfForm.changeFieldAttr("field22832", 3);

                        }

                    },
                    error: function(data) { //请求失败时被调用的函数
                        alert("加载失败，请联系管理员！")
                    }
                })

            }
        }

        return true;

    }

})